﻿using Grpc.Core;
using Manon.Core.Exception;
using Manon.Core.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Linq;

namespace Manon.Core.Commons
{
    internal class ApiResult
    {
        /// <summary>
        /// 是否成功失败
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 状态码
        /// </summary>
        public int StatusCode { get; set; }

        /// <summary>
        /// 信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 返回值
        /// </summary>
        public object Data { get; set; }

        /// <summary>
        /// 统一响应编码
        /// </summary>
        public string Code { get; set; }



        public static ApiResult ErrorResult(ActionExecutingContext context, string _Exp = "")
        {
            string error = string.Empty;
            foreach (var key in context.ModelState.Keys)
            {
                var state = context.ModelState[key];
                if (state.Errors.Any())
                {
                    error = state.Errors.First().ErrorMessage;
                    break;
                }
            }
            return new ApiResult()
            {
                Success = false,
                // Exception = _Exp,
                Message = error,
                StatusCode = StateCode.Error.GetHashCode()

            };
        }

        public static ApiResult ErrorResult(ExceptionContext context)
        {
            object obj = false;
            var methInfo = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).MethodInfo.ToString();
            if (!string.IsNullOrEmpty(methInfo) && methInfo.Contains("LanChen.Repository.Collections.PagedList", System.StringComparison.CurrentCultureIgnoreCase))
            {
                obj = new Manon.Core.Commons.PagedList();
            }

            string msg = context.Exception.Message;
            bool isBusinessExp = false;
            if (context.Exception is RpcException)
            {
                var grpcExp = (Grpc.Core.RpcException)context.Exception;
                var grpcStatus = grpcExp?.Status;
                 
                msg = grpcStatus?.Detail;
            }
            var result = new ApiResult()
            {
                Success = false,
                Message = msg,
                StatusCode = isBusinessExp ? StateCode.Ok.GetHashCode() : StateCode.Error.GetHashCode(),
                Data = obj
            };
            // 设置为true，表示异常已经被处理了
            context.ExceptionHandled = true;
            return result;
        }

        public static ApiResult SuccessResult(ResultExecutingContext context, object data)
        {

            return new ApiResult()
            {
                Success = true,
                StatusCode = StateCode.Ok.GetHashCode(),
                Data = data,
                //  Exception = "",
                Message = "请求成功"
            };
        }
    }
}
